<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <script src="../js/vue@3.3.4.global.js"></script>
    <script src="../../js/axios.js"></script>
</head>
<body>
    <div id="app">
        <form action="#" >
            <input  type="file" id="avatar" >
            <button type="button" @click="upload">上传</button>
        </form>
    </div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return {
                
            }
        },
        methods:{
            upload(){
                console.log("文件上传")
                let fileInput = document.querySelector('#avatar');
                if(fileInput.files.length==0){
                    alert('请选择文件');
                    return;
                }
                let formData = new FormData();
                formData.append("avatar",fileInput.files[0])
                axios.put('/api/user/avatar',formData,{
                    headers:{
                        Authorization: localStorage.getItem("token")
                    }
                }).then(resp=>{
                    console.log(resp)
                }).catch(error=>{
                    console.log(error)
                });
            }
        }
    })
    app.mount('#app')
</script>
</html>